//
//  UIImageView+SDWebCache.m
//  SDWebData
//
//  Created by stm on 11-7-13.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "SDImageView+SDWebCache.h"

@implementation UIImageView(SDWebCacheCategory)

- (void)setImageWithURL:(NSURL *)url imageType:(NSString *)imageType
{
	//[self setImageWithURL:url refreshCache:NO imageType:imageType];
    [self setImageWithURL:url refreshCache:NO placeholderImage:[UIImage imageNamed:@"bg_imageView.png"] imageType:imageType];
}

- (void)setImageWithURL:(NSURL *)url refreshCache:(BOOL)refreshCache imageType:(NSString *)imageType
{
	[self setImageWithURL:url refreshCache:refreshCache placeholderImage:nil imageType:imageType];
}

- (void)setImageWithURL:(NSURL *)url refreshCache:(BOOL)refreshCache placeholderImage:(UIImage *)placeholder imageType:(NSString *)imageType
{
    SDWebDataManager *manager = [SDWebDataManager sharedManager:imageType];
	
    // Remove in progress downloader from queue
    [manager cancelForDelegate:self];
	if (placeholder != nil) {
        self.image = placeholder;
    }
    
    

	
    if (url)
    {
        
//        UIActivityIndicatorView *activityView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
//        activityView.tag = 110;
//        [activityView startAnimating];
//        activityView.center = CGPointMake(CGRectGetWidth(self.frame) /2, CGRectGetHeight(self.frame) /2);
//        [self addSubview:activityView];
//        [activityView release];
        
        [manager downloadWithURL:url delegate:self refreshCache:refreshCache];
    }
}

- (void)cancelCurrentImageLoad :(NSString *)imageType
{
    [[SDWebDataManager sharedManager: imageType] cancelForDelegate:self];
}


- (NSData *)dataFromKey:(NSString *)key imageType:(NSString *)imageType{
    
    SDDataCache *dataCache = [SDDataCache sharedDataCache:imageType];
    NSData *data = [dataCache dataFromKey:key];
    
    return data;
}

#pragma mark -
#pragma mark SDWebDataManagerDelegate

- (void)webDataManager:(SDWebDataManager *)dataManager didFinishWithData:(NSData *)aData isCache:(BOOL)isCache
{
    
    if (aData) {
        UIImage *img=[UIImage imageWithData:aData];
        if (img) {
        
            self.image=img;
        
        }

    }
       
}

@end
